package com.cw.serviceimpl;

import com.cw.mapper.TbContentCategoryMapper;
import com.cw.mapper.TbContentMapper;
import com.cw.pojo.TbContent;
import com.cw.pojo.TbContentCategory;
import com.cw.pojo.TbContentExample;
import com.cw.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@com.alibaba.dubbo.config.annotation.Service
public class ContentServiceImpl implements ContentService {

    @Autowired
    private TbContentCategoryMapper contentCategoryMapper;

    @Autowired
    private TbContentMapper tbContentMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<TbContentCategory> getContentCategoryInfo(String name) {

        List<TbContentCategory> categoryList = contentCategoryMapper.getContentCategoryInfo(name);

        return categoryList;
    }

    @Override
    public void addContentCategory(TbContentCategory tbContentCategory) {

        contentCategoryMapper.insertSelective(tbContentCategory);
    }

    @Override
    public TbContentCategory getContentCategory(Long id) {
        return contentCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateContentCategory(TbContentCategory tbContentCategory) {
        contentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
    }

    @Override
    public void deleteContentCategory(Long[] ids) {

        contentCategoryMapper.deleteContentCategory(ids);
    }

    @Override
    public List<TbContent> getContentInfo() {
        return tbContentMapper.selectByExample(null);
    }

    @Override
    public List<TbContentCategory> getCategory() {
        return contentCategoryMapper.selectByExample(null);
    }

    @Override
    public void addContent(TbContent tbContent) {
        tbContentMapper.insertSelective(tbContent);
    }

    @Override
    public TbContent getContent(Long id) {
        return tbContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateContent(TbContent tbContent) {
        tbContentMapper.updateByPrimaryKeySelective(tbContent);
    }

    @Override
    public void deleteContent(Long[] ids) {
        tbContentMapper.deleteContent(ids);
    }

    @Override
    public List<TbContent> getContentList(Long categoryId) {

        BoundHashOperations picture = redisTemplate.boundHashOps("picture");

        String field = "pic" + categoryId;

        if (picture.hasKey(field)) {

            List<TbContent> contentList = (List<TbContent>) picture.get(field);

            return contentList;
        }else {

            List<TbContent> contentList = tbContentMapper.getContentList(categoryId);

            picture.put(field, contentList);

            return contentList;
        }

    }
}
